﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Diploma.Models;
using DiplomaWeb.TimeLines;

namespace DiplomaWeb.Controllers
{
    [Authorize]
    public class TimeLineController : MasterController
    {
        public TimeLineController(DataManager dataManager) : base(dataManager)
        {
        }

        public JsonResult ForSysManager(int planingTypeId, int levelId, int? scale)
        {
            PlaningType planingType = DataManager.PlaningsRepository.GetPlaningType(planingTypeId);
            QualificationLevel level = DataManager.QualificationLevelsRepository.GetLevel(levelId);
            if (planingType == null || level == null)
                throw new Exception();

            IEnumerable<GlobalTask> checkPoints =
                (from c in DataManager.QualificationLevelsRepository.GetGlobalTasks(level)
                 where c.PlaningTypeId == planingTypeId
                 select c);

            return GetTimeLine(checkPoints, scale, null);
        }

        public JsonResult ForRoom(int planingTypeId, Guid id, int? scale)
        {
            CollaborationRoom room = DataManager.CollaborationRoomRepository.GetCollaborationRoom(id);
            if (room == null)
                throw new Exception();

            IEnumerable<Task> tasks =
                DataManager.CollaborationRoomRepository.GetTasks(room).Where(
                    t => t.GlobalTask.PlaningTypeId == planingTypeId);
            IEnumerable<GlobalTask> globalTasks =
                DataManager.GlobalTasksRepository.GetGlobalTaskByLevelAndPlaningType(
                    room.Student.StudentsGroup.QualificationLevelId.Value, planingTypeId);

            return GetTimeLine(tasks.Cast<CheckPoint>().Union(globalTasks.Cast<CheckPoint>()), scale, room.Student);
        }

        [NonAction]
        private JsonResult GetTimeLine(IEnumerable<CheckPoint> checkPoints, int? scale, Student student)
        {
            var timeline = new TimeLine(checkPoints);
            IEnumerable<TimeLineItem> timeLineItems = timeline.GetTimeline(scale, student);
            return Json(timeLineItems, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetTimeLinesContainers()
        {
            ViewData.Model = DataManager.PlaningsRepository.GetAllPlaningTypes();
            return PartialView("~/Views/Planings/TimeLinesListControl.ascx");
        }

        public ActionResult LoadPlaningsControl(Guid? roomId)
        {
            if (roomId != null)
            {
                CollaborationRoom room = DataManager.CollaborationRoomRepository.GetCollaborationRoom(roomId.Value);
                if (room == null)
                    throw new Exception();
                ViewData.Model = room;
            }
            ViewData["LoggedUser"] = LoggedUser;
            return PartialView("~/Views/Planings/PlaningsControl.ascx");
        }
    }
}